import {Module, NestModule, MiddlewareConsumer, ValidationPipe, Dependencies} from '@nestjs/common';
import {APP_PIPE, APP_GUARD} from "@nestjs/core";
import {TypeOrmModule} from '@nestjs/typeorm';
import {DataSource} from 'typeorm';
import {ConfigModule} from '@nestjs/config';
import {EventEmitterModule} from '@nestjs/event-emitter';
import {CatsModule} from './cats/cats.module';
import {UsersModule} from './users/users.module';
import {AuthModule} from "./auth/auth.module";
import {AppController} from './app.controller';
import {FileController} from "./cats/files.controller";
import {AppService} from './app.service';
import {LoggerMiddleware} from "./common/middleware/logger.middleware";
// import {ValidationPipe} from "./common/pipe/validate.pipe";  // 这是重写演示-用common里面的方法
import {RolesGuard} from "./common/guard/roles.guard";

import {User} from "./users/entities/user.entity";

@Dependencies(DataSource)
@Module({
    imports: [
        ConfigModule.forRoot(),
        EventEmitterModule.forRoot(),
        TypeOrmModule.forRoot({
            type: 'mysql',
            host: 'localhost',
            port: 3306,
            username: 'root',
            password: 'root',
            database: 'test',
            entities: [User],
            synchronize: true,
            autoLoadEntities: true,
        }),
        CatsModule,
        UsersModule,
        AuthModule,
    ],
    controllers: [AppController, FileController],
    providers: [
        AppService,
        {
            provide: APP_PIPE,
            useClass: ValidationPipe
        },
        {
            provide: APP_GUARD,
            useClass: RolesGuard,
        },
    ],
    exports: []
})
export class AppModule implements NestModule {
    configure(consumer: MiddlewareConsumer): any {
        consumer
            .apply(LoggerMiddleware)
            .forRoutes('cats')
    }
}
